<template>
  <view class="custom-input-modal" v-if="visible">
    <!-- 遮罩层 -->
    <view class="modal-mask" @click.self="handleMaskClick"></view>

    <!-- 弹窗内容 -->
    <view class="modal-content">
      <text class="modal-title">{{ title }}</text>
      <input
          class="modal-input"
          type="text"
          :placeholder="placeholder"
          v-model="inputValue"
          auto-focus
          @confirm="handleSubmit"
      />
      <view class="modal-actions">
        <button class="btn-cancel" @click="handleCancel">取消</button>
        <button class="btn-confirm" @click="handleSubmit">确认</button>
      </view>
    </view>
  </view>
</template>

<script>export default {
  name: 'CustomInputModal',
  props: {
    // 控制是否显示
    visible: {
      type: Boolean,
      default: false
    },
    // 弹窗标题
    title: {
      type: String,
      default: '请输入'
    },
    // 输入框 placeholder 提示
    placeholder: {
      type: String,
      default: ''
    },
    // 默认值
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      inputValue: this.value
    };
  },
  watch: {
    value(newVal) {
      this.inputValue = newVal;
    },
    visible(newVal) {
      if (newVal && this.autoFocus) {
        // 模拟自动聚焦（UniApp input 不支持 autoFocus）
        uni.createSelectorQuery()
            .select('.modal-input')
            .context(this)
            .exec((res) => {
              if (res && res[0] && res[0].context) {
                res[0].context.focus();
              }
            });
      }
    }
  },
  methods: {
    handleCancel() {
      this.$emit('cancel');
    },
    handleSubmit() {
      this.$emit('confirm', this.inputValue.trim());
    },
    handleMaskClick() {
      if (!this.disableMaskClose) {
        this.handleCancel();
      }
    }
  }
};
</script>

<style lang="less">.custom-input-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9998;
}

.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 9998;
}

.modal-content {
  position: fixed;
  width: 600rpx;
  background-color: #fff;
  border-radius: 12rpx;
  padding: 40rpx;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 9999;
}

.modal-title {
  font-size: 32rpx;
  font-weight: bold;
  display: block;
  margin-bottom: 40rpx;
  text-align: center;
}

.modal-input {
  width: 100%;
  height: 80rpx;
  line-height: 80rpx;
  border: 1px solid #ddd;
  border-radius: 8rpx;
  padding: 0 20rpx;
  font-size: 28rpx;
  box-sizing: border-box;
  margin-bottom: 40rpx;
}

.modal-actions {
  display: flex;
  justify-content: space-between;
}

.btn-cancel,
.btn-confirm {
  flex: 1;
  font-size: 28rpx;
  padding: 16rpx 0;
  border-radius: 8rpx;
  margin: 0 10rpx;
}

.btn-cancel {
  color: #333;
  background-color: #f5f5f5;
}

.btn-confirm {
  color: white;
  background-color: #f1815b;
}
</style>
